import {
	requestURL
} from './config'


let header = {
	"content-type": "application/x-www-form-urlencoded;charset=UTF-8"
}

function getType(data, method) { // 获取请求方式
	let type = {}
	if (method === 'GET') {
		type = {
			method,
			dataType: 'json',
			data: JSON.stringify({ ...data
			}),
			header: {
				...header,
				"content-type": "application/json;charset=UTF-8",
				'Authorization': "Bearer " + uni.getStorageSync('userInfo100th').token
			}
		}
	} else {
		method = 'POST'
		type = {
			method,
			dataType: 'json',
			data: { ...data
			},
			header: {
				...header,
				'Authorization': "Bearer " + uni.getStorageSync('userInfo100th').token
			}
		}
	}
	return type
}

function Request(data, url, method) {
	console.log(JSON.stringify(data));
	return new Promise((resolve, reject) => {
		uni.showLoading()
		uni.request({
			url: requestURL + url,
			...getType(data, method),
			success: res => {
				uni.hideLoading()
				console.log(requestURL + url + '==')
				console.log(res)
				let data = res.data;
				if (data.code === 501 ||data.code === 500){
					console.log("接口调用凭证已失效")
					uni.clearStorageSync();
					uni.redirectTo({
						url: '/pages/index/index'
					})
				} else {
					resolve(res)
				}
			},
			fail: err => {
				uni.hideLoading()
				reject(err)
			}
		})
	}).catch(err => console.log(err))
}


export default function(data, url, method) {
	return Request(data, url, method)
}
